package com.pureut.storage.controller;


import com.pureut.common.core.web.controller.BaseController;
import com.pureut.common.core.web.domain.AjaxResult;
import com.pureut.common.core.web.page.TableDataInfo;
import com.pureut.common.log.annotation.Log;
import com.pureut.common.log.enums.BusinessType;
import com.pureut.common.security.annotation.RequiresPermissions;
import com.pureut.storage.dto.AllotManagementDto;
import com.pureut.storage.dto.AndroidPdaDto;
import com.pureut.storage.export.AllotManagementExport;
import com.pureut.storage.service.AllotManagementService;
import com.pureut.storage.util.ExcelUtil;
import com.pureut.storage.vo.*;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @author: CM
 * @date: 2023/1/10  21:27
 */
@RestController
@RequestMapping("/allotManagement")
public class AllotManagementController extends BaseController {

    @Resource
    AllotManagementService allotManagementService;

    /**
     *  新增调拨单
     */
    @RequiresPermissions("warehouse:inside:transfer:add")
    @Log(title = "新增调拨单", businessType = BusinessType.INSERT)
    @PostMapping("/addAllot")
    public AjaxResult addAllot(@RequestBody AllotManagementVo allotManagementVo) throws Exception {
        return toAjax(allotManagementService.addAllot(allotManagementVo));
    }


    /**
     * 调拨列表
     */
    @RequiresPermissions("warehouse:inside:transfer:list")
    @GetMapping("/list")
    public TableDataInfo getList(AllotManagementVo allotManagementVo) {
        startPage();
        //查询库存列表
        List<AllotManagementDto> list = allotManagementService.getList(allotManagementVo);
        return getDataTable(list);
    }

    /**
     * 修改调拨单
     */
    @RequiresPermissions("warehouse:inside:transfer:edit")
    @Log(title = "修改调拨单", businessType = BusinessType.UPDATE)
    @PostMapping("/updateAllot")
    public AjaxResult updateAllot(@RequestBody AllotManagementVo allotManagementVo) throws Exception {
        return toAjax(allotManagementService.updateAllot(allotManagementVo));
    }

    /**
     * 查看单据
     */
    @RequiresPermissions("warehouse:inside:transfer:query")
    @GetMapping("/view/{id}")
    public AjaxResult view(@PathVariable("id") Long id) throws Exception {
        return AjaxResult.success(allotManagementService.view(id));
    }

    /**
     * 删除单据
     */
    @RequiresPermissions("warehouse:inside:transfer:remove")
    @Log(title = "删除单据", businessType = BusinessType.DELETE)
    @DeleteMapping("/deleteAllot/{ids}")
    public AjaxResult deleteAllot(@PathVariable("ids") String ids) throws Exception {
        return toAjax(allotManagementService.deleteAllot(ids));
    }

    /**
     * 关结单据
     */
    @RequiresPermissions("warehouse:inside:transfer:close")
    @Log(title = "关结返工出库单", businessType = BusinessType.UPDATE)
    @PostMapping("/closeAllot")
    public AjaxResult closeAllot(@RequestBody AllotManagementVo allotManagementVo) throws Exception {
        return toAjax(allotManagementService.closeAllot(allotManagementVo));
    }

    /**
     * 导出
     */
    @RequiresPermissions("warehouse:inside:transfer:export")
    @PostMapping("/export")
    public AjaxResult inspectionMessageExport(HttpServletResponse response, AllotManagementVo allotManagementVo) throws IOException {
        List<AllotManagementExport> infoExport = allotManagementService.getInfoExport(allotManagementVo);
        ExcelUtil<AllotManagementExport> excelUtil = new ExcelUtil<AllotManagementExport>(AllotManagementExport.class);
        excelUtil.excelWriter(response, infoExport, "调拨单信息.xlsx");
        return AjaxResult.success();
    }

    /**
     * 调拨出库
     */
    @RequiresPermissions("warehouse:inside:transfer:transfer")
    @Log(title = "调拨出库", businessType = BusinessType.UPDATE)
    @PostMapping("/outAllot")
    public AjaxResult outAllot(@RequestBody AllotDbVo allotDbVo) throws Exception {
        return AjaxResult.success(allotManagementService.outAllot(allotDbVo));
    }

    /**
     * 调拨入库
     */
    @RequiresPermissions("warehouse:inside:transfer:transfer")
    @Log(title = "调拨入库", businessType = BusinessType.UPDATE)
    @PostMapping("/inAllot")
    public AjaxResult inAllot(@RequestBody AllotDbVo allotDbVo) throws Exception {
        return AjaxResult.success(allotManagementService.inAllot(allotDbVo));
    }

    /**
     * 拣货完成出库
     */
    @GetMapping("/finishAllot/{id}")
    public AjaxResult finishAllot(@PathVariable("id") Long id) throws Exception {
        return AjaxResult.success(allotManagementService.finishAllot(id));
    }

    /**
     * 拣货完成入库
     */
    @GetMapping("/finishAllotIn/{id}")
    public AjaxResult finishAllotIn(@PathVariable("id") Long id) throws Exception {
        return AjaxResult.success(allotManagementService.finishAllotIn(id));
    }

    /**
     * 拣货撤销
     */
    @GetMapping("/repealAllot/{id}")
    public AjaxResult repealAllot(@PathVariable("id") Long id) throws Exception {
        return AjaxResult.success(allotManagementService.repealAllot(id));
    }

    /**
     * 出库交接
     */
    @RequiresPermissions("warehouse:inside:transfer:handover")
    @PostMapping("/overAllot")
    public AjaxResult overAllot(@RequestBody PickingRestDbVo pickingRestDbVo) throws Exception {
        return AjaxResult.success(allotManagementService.overAllot(pickingRestDbVo));
    }

    /**
     * 调拨单列表Pda
     */
    @GetMapping("/allotPdaList")
    public TableDataInfo allotPdaList(AllotPdaListVo allotPdaListVo) {
        startPage();
        //查询库存列表
        List<AndroidPdaDto> list = allotManagementService.allotPdaList(allotPdaListVo);
        return getDataTable(list);
    }

    /**
     * 调拨单列表Pda详情
     */
    @GetMapping("/allotPdaView")
    public AjaxResult allotPdaView(AllotPdaListVo allotPdaListVo) throws Exception {
        return AjaxResult.success(allotManagementService.allotPdaView(allotPdaListVo));
    }

    /**
     * 修改检验单状态Feign
     */
    @Log(title = "修改单据", businessType = BusinessType.UPDATE)
    @PostMapping("/allotUpdateStatus")
    public AjaxResult allotUpdateStatus(@RequestBody UpdateDocVo updateDocVo) throws Exception {
        return toAjax(allotManagementService.allotUpdateStatus(updateDocVo));
    }

}
